package com.project.controller;

import com.project.dto.AddInventoryDTO;
import com.project.dto.AdjustInventoryDTO;
import com.project.dto.InventoryQueryDTO;
import com.project.result.PageResult;
import com.project.result.Result;
import com.project.service.InventoryService;
import com.project.vo.InventoryDetailVO;
import com.project.vo.InventoryStatisticsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 库存管理
 */
@RestController
@RequestMapping("/inventory")
@Slf4j
public class InventoryController {
    @Autowired
    private InventoryService inventoryService;

    /**
     * 分页获取材料库存列表
     * @param queryDTO
     * @return
     */
    @GetMapping
    public Result queryInventoryPage(InventoryQueryDTO queryDTO) {
        log.info("分页获取库存列表：{}", queryDTO);
        PageResult page = inventoryService.queryInventoryPage(queryDTO);
        return Result.success(page);
    }

    /**
     * 根据材料ID获取库存信息
     * @param materialId
     * @return
     */
    @GetMapping("/material/{materialId}")
    public Result<InventoryDetailVO> queryInventoryById(@PathVariable Integer materialId) {
        log.info("根据材料ID查询库存：{}", materialId);
        InventoryDetailVO inventory = inventoryService.getMaterialById(materialId);
        return Result.success(inventory);
    }


    /**
     * 先查看库存信息
     * @param inventoryId
     * @return
     */
    @GetMapping("/{inventoryId}")
    public Result<InventoryDetailVO> getInventoryById(@PathVariable Integer inventoryId) {
        log.info("根据库存ID查询库存：{}", inventoryId);
        InventoryDetailVO inventory = inventoryService.getInventoryById(inventoryId);
        return Result.success(inventory);
    }

    /**
     * 再修改库存数量
     * @param inventoryId
     */
    @PutMapping("/{inventoryId}/adjust")
    public Result updateInventory(@PathVariable Integer inventoryId, @RequestBody AdjustInventoryDTO inventoryDTO) {
        log.info("修改库存数量：{},{}", inventoryId, inventoryDTO);
        inventoryService.updateInventoryById(inventoryId, inventoryDTO);
        return Result.success();
    }

    /**
     * 删除库存信息
     * @param inventoryId
     * @return
     */
    @DeleteMapping("/{inventoryId}")
    public Result deleteInventory(@PathVariable Integer inventoryId) {
        log.info("删除库存信息：{}", inventoryId);
        boolean result = inventoryService.removeById(inventoryId);
        return result ? Result.success() : Result.error("删除失败");
    }

    /**
     * 新增
     * @param addDTO
     * @return
     */
    @PostMapping
    public Result addInventory(@RequestBody AddInventoryDTO addDTO) {
        log.info("新增库存信息：{}", addDTO);
        boolean result = inventoryService.addInventory(addDTO);
        return result ? Result.success() : Result.error("新增失败");
    }

    /**
     * 获取库存统计信息
     * @return
     */
    @GetMapping("/statistics")
    public Result<InventoryStatisticsVO> getInventoryStatistics(Integer categoryId) {
        InventoryStatisticsVO inventoryStatistics = inventoryService.getInventoryStatistics(categoryId);
        return Result.success(inventoryStatistics);
    }

}